<template>
  <div>
    <template>
      <Card style="width:850px;margin: auto;">
        <Form :model="formLeft" label-position="left" :rules="validateLibForm" :label-width="150"
              style="width: 300px;margin: auto;">


          <FormItem label="原材料材质分类类别名称:" prop="materialClassificationCode">
            <Select v-model="formLeft.materialClassificationCode">
              <Option v-for="item in materialClassificationList" :value="item.code" :key="item.code">{{ item.name }}
              </Option>
            </Select>
          </FormItem>


          <FormItem label="原材料材质分类类别代码:" prop="code">
            <Input v-model="formLeft.code" :maxlength="2" @on-keyup="Alphanumeric"
                   @on-keydown="Alphanumeric"  @on-blur="Alphanumeric"></Input>
          </FormItem>

          <FormItem label="原材料材质分类类别名称:" prop="name">
            <Input v-model="formLeft.name" :maxlength="10"></Input>
          </FormItem>

          <row type="flex" justify="center">
            <i-col span="20">

            </i-col>
            <i-col span="30">
              <Button type="primary" @click="CategoryInsert">添加</Button>
              <Button type="success" style="margin-left: 160px" @click="back">返回</Button>
            </i-col>
          </row>

        </Form>
      </Card>
    </template>
  </div>
</template>

<script>
  import {CategoryInsert, MaterialClassificationFindAll} from "../../../api/data"

  export default {
    data() {
      return {
        materialClassificationList: [],
        formLeft: {
          code: '',
          name: '',
          makingsState: '原材料',
          materialClassificationCode: '',
        },
        validateLibForm: {
          code: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          name: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          materialClassificationCode: [
            {required: true, message: '请选择', trigger: 'blur'}
          ],
        }
      }
    },
    mounted() {
      this.MaterialClassificationFindAll()
    },
    methods: {
      Alphanumeric(e) {
        e.target.value = e.target.value.replace(/[^\w]/g,"");
      },
      MaterialClassificationFindAll() {
        var data = {
          page: 1,
          pageSize: 1000,
          state: '1'
        };
        MaterialClassificationFindAll(data).then(data => {
          if (data.data.code == 0) {
            this.materialClassificationList = data.data.data.list;
          }
        })
      },
      CategoryInsert() {
        if (this.formLeft.materialClassificationCode !== null && this.formLeft.materialClassificationCode !== "") {
          if (this.formLeft.code !== null && this.formLeft.code !== "") {
            if (this.formLeft.code.length == 2) {
              if (this.formLeft.name !== null && this.formLeft.name !== "") {
                CategoryInsert(this.formLeft).then(res => {
                  if (res.data.code == 0) {
                    this.$Message.success("编辑成功");
                    this.$router.go(-1);
                  } else {
                    this.$Message.error(res.data.msg);
                  }
                })
              } else {
                this.$Message.error("原材料材质分类名称不能为空");
              }
            } else {
              this.$Message.error("原材料材质分类代码格式不满足要求");
            }
          } else {
            this.$Message.error("原材料材质分类代码不能为空");
          }
        } else {
          this.$Message.error("原材料材质分类类别名称不能为空");
        }
      },
      back() {
        this.$router.go(-1);
      },

    }
  }
</script>


